package com.mcflies.ui.asynLoadImage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.mcflies.commons.McfliesConstant;
import com.mcflies.ui.R;
import com.mcflies.ui.asynLoadImage.LazyScrollView.OnScrollListener;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-21
 * @comment:瀑布式加载图片
 */
public class AsynLoadImageActivity extends Activity {
	private Display display;
	private int columnItemWidth;// 没列每项的宽度
	private AssetManager assetManager;

	private LazyScrollView asynLoadImage_LZV;
	private LinearLayout asynLoadImage_LL;
	private List<LinearLayout> asynLoadImage_LL_List;

	private List<String> imageFileNameList;
	private String imageFileDir = "asynLoadImageDir";// 存放图片的目录

	private int perColumnCount = 3;// 没列显示3张图片
	private int perPageCount = 15;// 每页显示15张图片
	private int currentPage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.android_asyn_load_image_main);
		initData();
		// 初始化布局
		initLayout();
	}

	private void initData() {
		display = this.getWindowManager().getDefaultDisplay();
		columnItemWidth = display.getWidth() / perColumnCount;// 获取屏幕宽度
		assetManager = this.getAssets();
		try {
			imageFileNameList = Arrays.asList(assetManager.list(imageFileDir));
		} catch (Exception e) {
			Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG, e.getMessage());
		}
	}

	private void initLayout() {
		asynLoadImage_LZV = (LazyScrollView) findViewById(R.id.asynLoadImage_LZV);
		asynLoadImage_LZV.getView();
		asynLoadImage_LZV.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onTop() {
			}

			@Override
			public void onScroll() {
			}

			@Override
			public void onBottom() {
				addImageToContainer(++currentPage, perPageCount);
			}
		});

		asynLoadImage_LL = (LinearLayout) findViewById(R.id.asynLoadImage_LL);

		asynLoadImage_LL_List = new ArrayList<LinearLayout>();
		for (int i = 0; i < perColumnCount; i++) {
			LinearLayout layout = new LinearLayout(this);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					columnItemWidth, LayoutParams.WRAP_CONTENT);
			layout.setPadding(2, 2, 2, 2);
			layout.setOrientation(LinearLayout.VERTICAL);
			layout.setLayoutParams(params);
			asynLoadImage_LL_List.add(layout);
			asynLoadImage_LL.addView(layout);
		}

		addImageToContainer(currentPage, perPageCount);
	}

	private void addImageToContainer(int currentPage, int pageCount) {
		int curCol = 0;// 当前列
		int imageCount = imageFileNameList.size();
		for (int i = currentPage * pageCount; i < pageCount * (currentPage + 1)
				&& i < imageCount; i++) {
			curCol = curCol >= perColumnCount ? curCol = 0 : curCol;
			addImage(imageFileNameList.get(i), curCol++);
		}
	}

	private void addImage(String imageName, int colIndex) {
		ImageView imageView = (ImageView) LayoutInflater.from(this).inflate(
				R.layout.android_asyn_load_image_item, null);
		asynLoadImage_LL_List.get(colIndex).addView(imageView);

		TaskParam param = new TaskParam();
		param.setAssetManager(assetManager);
		param.setFilename(imageFileDir + "/" + imageName);
		param.setItemWidth(columnItemWidth);

		ImageLoaderTask task = new ImageLoaderTask(imageView);
		task.execute(param);
	}
}
